﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

//STATS MODULE: DATA-OBJECTS
//



namespace Stats.Objects
{

    /// <summary>
    /// this class parse the out-of-project updated datas from a xml file 
    /// </summary>
    public class ConfigurationData
    {
        /// <summary>
        /// The fuel cost per liter used in TripLOA.
        /// </summary>
        public double FuelCostPerLiterReference { get; set; }

        /// <summary>
        /// Specifies the emission measure division unit...now emissions are returned in quintals of Co2
        /// </summary>
        public int EmissionMeasureUnitDivision { get; set; }

        #region vehicles configuration variables

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Utilitary cars
        /// </summary>
        public float UtilitaryCarsFuelConsumptionReference { get; set; }

        /// <summary>
        /// This variable contains the emissions reference values for the vehicle types: Utilitary cars
        /// </summary>
        public int UtilitaryCarsEmissionsReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Sedan cars
        /// </summary>
        public float SedanCarsFuelConsumptionReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Sedan cars
        /// </summary>
        public int SedanCarsEmissionsReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Sport cars
        /// </summary>
        public float SportCarsFuelConsumptionReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Sport cars
        /// </summary>
        public int SportCarsEmissionsReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: SUV cars
        /// </summary>
        public float SUVCarsFuelConsumptionReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: SUV cars
        /// </summary>
        public int SUVCarsEmissionsReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: MPV cars
        /// </summary>
        public float MPVCarsFuelConsumptionReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: MPV cars
        /// </summary>
        public int MPVCarsEmissionsReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Motorbikes
        /// </summary>
        public float MotorbikeFuelConsumptionReference { get; set; }

        /// <summary>
        /// This variable contains the fuel consumption reference values for the vehicle types: Motorbike
        /// </summary>
        public int MotorbikeEmissionsReference { get; set; }

        #endregion

        #region pending feedbacks configuration variables
        
        /// <summary>
        /// These days after the trip departure date, a pending feedback expires and is removed from PendingFeedback table
        /// </summary>
        public int PendingFeedback_DisplayAfterDays { get; set; }

        /// <summary>
        /// Specifies after how many days from trip departure date the pending feedbacks are visible and can be left
        /// </summary>
        public int PendingFeedback_ExpireDays { get; set; }

        /// <summary>
        /// Specifies the max number of trips to return with pending feedbacks
        /// </summary>
        public int MaxNumberOfUserTripsWithPendingFeedbacksToReturn { get; set; }

        #endregion

        /// <summary>
        /// Constructor for the ConfigurationData object.
        /// </summary>
        /// <param name="dataFilePath">The path of the XML configuration file</param>
        public ConfigurationData(string dataFilePath)
        {
            if (dataFilePath == null) throw new InvalidOperationException("dataFilePath must be set befor calling ConfigurationData constructor");
            
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;

            XmlReader reader = XmlReader.Create(dataFilePath, settings);
                       
            String attr = "";

            while (reader.Read())
            {

                attr = reader.GetAttribute("var");

                switch (attr)
                {
                    case "FuelCost":
                        FuelCostPerLiterReference = double.Parse(reader.GetAttribute("value"));
                        break;

                    case "EmissionMeasureUnitDivision":
                        EmissionMeasureUnitDivision = int.Parse(reader.GetAttribute("value"));
                        break;

                    #region Generic vehicles fuel consumption and emissions

                    case "UtilitaryCarsF":
                        UtilitaryCarsFuelConsumptionReference = float.Parse(reader.GetAttribute("value"));
                        break;
                    case "UtilitaryCarsE":
                        UtilitaryCarsEmissionsReference = int.Parse( reader.GetAttribute("value"));
                        break;
                    
                    case "SedanCarsF":
                        SedanCarsFuelConsumptionReference = float.Parse( reader.GetAttribute("value"));
                        break;
                    case "SedanCarsE":
                        SedanCarsEmissionsReference = int.Parse( reader.GetAttribute("value"));
                        break;

                    case "SportCarsF":
                        SportCarsFuelConsumptionReference = float.Parse( reader.GetAttribute("value"));
                        break;
                    case "SportCarsE":
                        SportCarsEmissionsReference = int.Parse( reader.GetAttribute("value"));
                        break;

                    case "SUVCarsF":
                        SUVCarsFuelConsumptionReference = float.Parse( reader.GetAttribute("value"));
                        break;
                    case "SUVCarsE":
                        SUVCarsEmissionsReference = int.Parse( reader.GetAttribute("value"));
                        break;

                    case "MPVCarsF":
                        MPVCarsFuelConsumptionReference = float.Parse( reader.GetAttribute("value"));
                        break;
                    case "MPVCarsE":
                        MPVCarsEmissionsReference = int.Parse( reader.GetAttribute("value"));
                        break;

                    case "MotorbikeF":
                        MotorbikeFuelConsumptionReference = float.Parse( reader.GetAttribute("value"));
                        break;
                    case "MotorbikeE":
                        MotorbikeEmissionsReference = int.Parse(reader.GetAttribute("value"));
                        break;

                    #endregion

                    #region Pending feedbacks configuration values

                    case "PendingFeedback_DisplayAfterDays":
                        PendingFeedback_DisplayAfterDays = int.Parse(reader.GetAttribute("value"));
                        break;

                    case "PendingFeedback_ExpireDays":
                        PendingFeedback_ExpireDays = int.Parse(reader.GetAttribute("value"));
                        break;

                    case "MaxNumberOfUserTripsWithPendingFeedbacksToReturn":
                        MaxNumberOfUserTripsWithPendingFeedbacksToReturn = int.Parse(reader.GetAttribute("value"));
                        break;

                    #endregion

                }

            }

        }

    }
}
